package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.dto.PmsProductCategoryParam;
import com.ftg.learn.mall.entity.PmsProductCategory;
import com.ftg.learn.mall.service.impl.PmsProductCategoryServiceImpl;
import com.ftg.learn.util.BaseController;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsProductCategory")
public class PmsProductCategoryController extends BaseController<PmsProductCategory,PmsProductCategoryServiceImpl> {

    @Autowired
    PmsProductCategoryServiceImpl pmsProductCategoryService;


    /**
     * 分页商品分类
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/pageFindProductCategory")
    public ResPageEntity pageFindProductCategory(@RequestParam(value = "page" ,defaultValue = "1") int page,
                                                 @RequestParam(value = "limit" ,defaultValue = "5") int limit) {
        Page<PmsProductCategory> pagination = new Page<>(page, limit);
        service.page(pagination);
        return new ResPageEntity(pagination);
    }

    /**
     * 按条件分页查询商品分类
     * @param column
     * @param val
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/pageFindProductCategoryByOne")
    public ResPageEntity pageFindProductCategoryByOne(@RequestParam String column,@RequestParam String val,
                                                      @RequestParam(value = "page" ,defaultValue = "1") int page,
                                                      @RequestParam(value = "limit" ,defaultValue = "5") int limit) {
        Page<PmsProductCategory> pagination = new Page<>(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(column,val);
        service.page(pagination,queryWrapper);
        return new ResPageEntity(pagination);
    }
    /**
     * 根据id获取父级下子级的分类
     * @return
     */
    @GetMapping("/findParentAndChild")
    public ResEntity findParentAndChild(@RequestParam Long id) {
        List<PmsProductCategoryParam> productCategory = pmsProductCategoryService.findParentAndChild(id);
        return productCategory.isEmpty() ? new ResEntity(-1, "次商品分类没有子级分类！")
                : new ResEntity(0, "success", productCategory);
    }

    /**
     * 根据id获取产品分类
     * @return
     */
    @GetMapping("/GetProductCategory")
    public ResEntity GetProductCategory(@RequestParam Long id) {
        PmsProductCategory productCategory = service.getById(id);
        return new ResEntity(0, "success", productCategory);
    }

    /**
     * 修改状态栏显示
     * @param ids
     * @return
     */
    @PostMapping("/updateNavStatus")
    public ResEntity updateNavStatus(@RequestParam List<Long> ids, @RequestParam Long navStatus){
        int count = pmsProductCategoryService.updateNavStatus(ids, navStatus);
        if (count > 0) {
            return new ResEntity(0, "成功修改"+count+"条");
        } else {
            return new ResEntity(-1, "修改失败");
        }
    }


    @PostMapping("/updateNavStatus1")
    public ResEntity updateNavStatus1(@RequestBody PmsProductCategory productCategory){
       super.service.updateById(productCategory);
        return new ResEntity(0,"success");
    }

    /**
     * 修改显示状态
     * @param ids
     * @return
     */
    @PostMapping("/updateShowStatus")
    public ResEntity updateShowStatus(@RequestParam List<Long> ids , @RequestParam Long showStatus){
        int count = pmsProductCategoryService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return new ResEntity(0, "成功修改"+count+"条");
        } else {
            return new ResEntity(-1, "修改失败");
        }
    }

    /**
     * 查询所有产品类型一级分类及子分类(级联查询)
     * @return
     */
    @GetMapping("/findWithChildren")
    public ResEntity findWithChildren(){
        return new ResEntity(0,"success",pmsProductCategoryService.findWithChildren());
    }

    /**
     * 查询所有产品类型一级分类及子分类(递归查询)
     * @return
     */
    @GetMapping("/findWithChildrenByRecursion")
    public ResEntity findWithChildrenByRecursion(){
        return new ResEntity(0,"success",pmsProductCategoryService.findWithChildrenByRecursion());
    }

}

